package com.xiaoshuidi.cloud.module.member.convert.securitycheckprogram;

import cn.hutool.core.util.ObjectUtil;
import com.xiaoshuidi.cloud.module.member.controller.admin.securitycheckprogram.vo.Gridders;
import com.xiaoshuidi.cloud.module.member.controller.admin.securitycheckprogram.vo.SecurityCheckProgramExcelVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.securitycheckprogram.vo.SecurityCheckProgramRespVO;
import com.xiaoshuidi.cloud.module.system.api.user.AdminUserApi;
import com.xiaoshuidi.cloud.module.system.api.user.dto.AdminUserRespDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Component
public class MySecurityCheckProgramConvert {

    @Resource
    private AdminUserApi adminUserApi;

    public List<SecurityCheckProgramExcelVO> convert(List<SecurityCheckProgramRespVO> list) {
        if (list == null) {
            return null;
        }

        List<SecurityCheckProgramExcelVO> list1 = new ArrayList<SecurityCheckProgramExcelVO>(list.size());
        List<Long> creatorIds = list.stream().filter(p -> ObjectUtil.isNotEmpty(p.getCreator())).map(p -> {
            return Long.valueOf(p.getCreator());
        }).distinct().collect(Collectors.toList());
        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(creatorIds);
        for (SecurityCheckProgramRespVO securityCheckProgramRespVO : list) {
            list1.add(securityCheckProgramToSecurityCheckProgramExcelVO(securityCheckProgramRespVO, userMap));
        }

        return list1;
    }

    protected static SecurityCheckProgramExcelVO securityCheckProgramToSecurityCheckProgramExcelVO(SecurityCheckProgramRespVO securityCheckProgramRespVO, Map<Long, AdminUserRespDTO> userMap) {
        if (securityCheckProgramRespVO == null) {
            return null;
        }

        SecurityCheckProgramExcelVO securityCheckProgramExcelVO = new SecurityCheckProgramExcelVO();

        securityCheckProgramExcelVO.setId(securityCheckProgramRespVO.getId());
        securityCheckProgramExcelVO.setCheckWorkUnit(securityCheckProgramRespVO.getCheckWorkUnit());
        securityCheckProgramExcelVO.setName(securityCheckProgramRespVO.getName());
        securityCheckProgramExcelVO.setStatus(securityCheckProgramRespVO.getStatus() ? "启用" : "停用");
        securityCheckProgramExcelVO.setRemark(securityCheckProgramRespVO.getRemark());
        securityCheckProgramExcelVO.setCreateTime(securityCheckProgramRespVO.getCreateTime());
        AdminUserRespDTO adminUserRespDTO = userMap.get(Long.valueOf(securityCheckProgramRespVO.getCreator()));
        securityCheckProgramExcelVO.setCreatorName(ObjectUtil.isNotEmpty(adminUserRespDTO) ? adminUserRespDTO.getNickname() : null);
        securityCheckProgramExcelVO.setGriddersName(securityCheckProgramRespVO.getGridders().stream().map(Gridders::getName).collect(Collectors.joining(",")));
        return securityCheckProgramExcelVO;
    }
}
